package com.zlp.mall.portal.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zlp.mall.common.api.R;
import com.zlp.mall.portal.config.SessionMap;
import com.zlp.mall.portal.modules.oms.dto.req.CarAddReq;
import com.zlp.mall.portal.modules.oms.dto.resp.CarItemResp;
import com.zlp.mall.portal.modules.oms.model.CartItem;
import com.zlp.mall.portal.modules.oms.service.CartItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 相册表 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-10-13
 */
@RestController
@RequestMapping("/car")
@RequiredArgsConstructor
@Api(value = "CarController",tags = "购物车模块")
public class CarController {

    private final CartItemService cartItemService;
    private final SessionMap sessionMap;


    @GetMapping("/products/sum")
    @ApiOperation(value = "获取购物商品总数量")
    public R<Integer> getCartSum(){

        return R.success(cartItemService.getCartSum());
    }

    @PostMapping(value = "/addCar")
    @ApiOperation(value = "添加购物车")
    public R<List<CarItemResp>> addCar(@Valid @RequestBody CarAddReq carAddReq) {

        List<CarItemResp> carItemRespList = cartItemService.addCar(carAddReq);
        return R.success(carItemRespList);
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "购物车列表")
    public R<List<CarItemResp>> cartList() {

        LambdaQueryWrapper<CartItem> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CartItem::getMemberId,sessionMap.getSessionUser().getId());
        queryWrapper.orderByDesc(CartItem::getId);
        queryWrapper.eq(CartItem::getDeleteStatus,1);
        return R.success(cartItemService.cartList(queryWrapper));
    }

    @PostMapping(value = "/update/quantity")
    @ApiOperation(value = "更新购物数量")
    public R<Boolean> updateQuantity(Long id,Integer quantity) {

        return R.success(cartItemService.updateQuantity(id,quantity));
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除购物车")
    public R<Boolean> delete(@RequestParam(value = "ids") List<Long> ids) {

        return R.success(cartItemService.delete(ids));
    }
}

